<?php

// +----------------------------------------------------------------------
// | Static Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-static
// | github 代码仓库：https://github.com/zoujingli/think-plugs-static
// +----------------------------------------------------------------------

namespace app\index\controller;

use think\admin\Controller;
use think\facade\Db;
use app\fx\model\DataBase;
use app\fx\model\DataProductCate;
use app\fx\model\DataProductShop;
use think\admin\extend\DataExtend;
use think\facade\Request;

class Index extends Controller
{
    protected function initialize()
    {

        $ip_str = DataBase::mk()->where('id', 2)->value('data');
        // $ip_arr=explode(",",$ip_str);
        // $ip=$this->request->ip();
        // if(!in_array($ip,$ip_arr)){echo $ip.'你暂无权限访问';exit();}

        $mcat_arr = DataProductCate::mk()->where('pid', 0)->where('status', 0)->column('*');
        foreach ($mcat_arr as $k => $v) {
            $cat1_arr = DataProductCate::mk()->where('pid', $v['id'])->where('status', 0)->column('*');
            foreach ($cat1_arr as $sk => $sv) {
                $cat2_arr = DataProductCate::mk()->where('pid', $sv['id'])->where('status', 0)->column('*');
                if (count($cat2_arr) > 0) {
                    $cat1_arr[$sk]['sub'] = $cat2_arr;
                }
            }
            $mcat_arr[$k]['sub'] = $cat1_arr;
        }
        $this->mcat_arr = $mcat_arr;

    }

    public function header()
    {

        $this->fetch();
    }

    public function index()
    {
        $this->fetch();
    }


    public static function cat_fun(array $its, string $cid = 'id', string $pid = 'pid', string $path = 'path'): array
    {
        $call = function (array $its, callable $call, array &$data = [], string $parent = '') use ($cid, $pid, $path) {
            foreach ($its as $it) {
                $ts = $it['sub'] ?? [];
                $it[$path] = "{$parent}-{$it[$cid]}";
                if ($it['pid'] == 0) {
                    $data[] = $it;
                }
                if (empty($ts))
                    continue;
                $call($ts, $call, $data, $it[$path]);
            }
            return $data;
        };
        return $call(static::cat_tree_fun($its, $cid, $pid), $call);
    }

    public static function cat_tree_fun(array $its, string $cid = 'id', string $pid = 'pid', string $sub = 'sub'): array
    {
        [$tree, $its] = [[], array_column($its, null, $cid)];
        foreach ($its as $it)
            isset($its[$it[$pid]]) ? $its[$it[$pid]][$sub][] = &$its[$it[$cid]] : $tree[] = &$its[$it[$cid]];
        return $tree;
    }

    // public function plist()
    // {
    //     $cat_pos = DataProductCate::mk()->where('status', 0)->column('*', 'id');
    //     $this->cat = input('cat', 0);
    //     if (!empty($cat_pos[$this->cat])) {
    //         $this->cat_name = $cat_pos[$this->cat]['name'];
    //     } else {
    //         $this->cat_name = "";
    //     }
    //     $this->fetch();
    // }

    public function plist_data()
    {

        $cat = input('cat', 0);
        $page = input('page', 1);
        $limit = input('limit', 20);
        $map = [];
        if (!empty($cat)) {
            $map['cat1|cat2|cat3'] = $cat;
        }
        $data = DataProductShop::mk()->where($map)->page($page, $limit)->column('*');
        foreach ($data as $k => $v) {
            if (!empty($v['slider'])) {
                $data[$k]['cover'] = explode("|", $v['slider'])[0];
            } else {
                $data[$k]['cover'] = "";
            }
        }
        $count = DataProductShop::mk()->where($map)->count();
        return json(['data' => $data, 'count' => $count]);
    }

    public function intro()
    {
        $id = input('id', 0);
        $infor = DataProductShop::mk()->where('id', $id)->find();
        $cat_pos = DataProductCate::mk()->where('status', 0)->column('*', 'id');
        $cat = $infor['cat3'];
        if (empty($cat)) {
            $cat = $infor['cat2'];
        }
        $this->cat_id = $cat;
        $this->cat_name = $cat_pos[$cat]['name'];

        if (empty($infor['slider'])) {
            $infor['slider'] = [];
        } else {
            $infor['slider'] = explode("|", $infor['slider']);
        }
        $this->tit_arr = ['中文名', '英文名', '商品中文规格', '商品英文规格', '计量单位组', '主计量单位', '采购计量单位', '销售计量单位', '库存计量单位', '报关计量单位', '报关助记符', '报关商品编码', '报关商品中文名', '报关商品英文名', '单箱净重', '单箱毛重', '单箱体积', '单箱数量', '箱子长', '箱子宽', '箱子高', '出口属性', '进口属性', '配件属性', '内销属性', '包材属性', '内购属性', '自制属性', '委外属性', '保税属性', 'OEM号', '颜色', '商标', '销售单价', '备注', '首选供应商', '默认采购币种', '默认采购单价', '拥有人', '生产说明', '增值税率%'];

        $this->id_arr = ['name', 'ename', 'guige', 'eguige', 'int', 'mint', 'bint', 'sint', 'tint', 'bgint', 'bgicon', 'bgcode', 'bgname', 'bgename', 'pweight', 'pmweight', 'ptj', 'pnum', 'plong', 'pkuan', 'pheight', 'cktype', 'jktype', 'pjtype', 'nxtype', 'mtype', 'ngtype', 'zztype', 'wwtype', 'bstype', 'oem', 'color', 'sb', 'price', 'beizhu', 'fgys', 'mbz', 'mprice', 'owner', 'des', 'ratio'];
        $this->infor = $infor;

        $this->fetch();

    }

    public function check_pass($id, $pass)
    {
        $spass = DataBase::mk()->where('id', 1)->value('data');
        if ($pass == $spass) {
            //$price=$this->check_price($id);
            $infor = DataProductShop::mk()->where('id', $id)->find();
            $res = ['code' => 'y', 'price' => $infor['price'], 'company' => $infor['company']];

        } else {
            $res = ['code' => 'n'];
        }
        return json($res);
    }

    public function check_price($id)
    {

        $infor = DataProductShop::mk()->where('id', $id)->find();
        $price = $infor['price'];
        return $price;
    }

    public function search_fun($txt)
    {
        $infor = DataProductShop::mk()->where('no', $txt)->find();
        if ($infor) {
            return json(['code' => 'y', 'id' => $infor['id']]);
        } else {
            return json(['code' => 'n']);
        }
    }

    /**
     * 获取产品数据
     * @param mixed $txt
     * @param mixed $cat
     * @param mixed $page
     * @param mixed $limit
     * @return \think\response\Json
     */
    public function queryProducts($txt, $cat, $page = 1, $limit = 20)
    {
        $map = [];
        $data = [];
        $count = 0;
        // 查询数据 | 
        if (!empty($cat)) {
            $map['cat1|cat2|cat3'] = $cat;
            $data = DataProductShop::mk()
                ->where($map)
                ->page($page, $limit)
                ->select()
                ->toArray();
            $count = DataProductShop::mk()
                ->where($map)
                ->count();
        } else {
            $data = DataProductShop::mk()
                ->where(function ($query) use ($txt) {
                    $query->where('no', 'like', "%{$txt}%")
                        ->whereOr('name', 'like', "%{$txt}%");
                })
                ->page($page, $limit)
                ->select()
                ->toArray();

            $count = DataProductShop::mk()
                ->where(function ($query) use ($txt) {
                    $query->where('no', 'like', "%{$txt}%")
                        ->whereOr('name', 'like', "%{$txt}%");
                })
                ->count();
        }

        // 处理图片
        foreach ($data as $k => $v) {
            $data[$k]['cover'] = !empty($v['slider']) ? explode("|", $v['slider'])[0] : "";
        }

        return json(['data' => $data, 'count' => $count]);
    }

    /**
     * 获取菜单
     * @return \think\response\Json
     */
    public function query_menu()
    {
        $mcat_arr = DataProductCate::mk()->where('pid', 0)->where('status', 0)->column('*');
        foreach ($mcat_arr as $k => $v) {
            $cat1_arr = DataProductCate::mk()->where('pid', $v['id'])->where('status', 0)->column('*');
            foreach ($cat1_arr as $sk => $sv) {
                $cat2_arr = DataProductCate::mk()->where('pid', $sv['id'])->where('status', 0)->column('*');
                if (count($cat2_arr) > 0) {
                    $cat1_arr[$sk]['sub'] = $cat2_arr;
                }
            }
            $mcat_arr[$k]['sub'] = $cat1_arr;
        }
        return json(['menus' => $mcat_arr]);
    }

    /**
     * 获取产品详情
     * @param mixed $id 产品编号
     * @return \think\response\Json
     */
    public function checkDetail($id)
    {
        $infor = DataProductShop::mk()->where('id', $id)->find();
        return json(['data' => $infor]);
    }

    /**
     * 加载产品页面
     * @return void
     */
    public function plist()
    {
        $this->redirect('/index/index/index');
    }
}
